#include <QtGui>

#include "renderarea.h"

RenderArea::RenderArea(QWidget *parent)
    : QWidget(parent)
{
    shape = Square;
    antialiased = true;
    transformed = false;

    setBackgroundRole(QPalette::Base);
    setAutoFillBackground(false);
}

QSize RenderArea::minimumSizeHint() const
{
    return QSize(671, 381);
}

QSize RenderArea::sizeHint() const
{
    return QSize(671, 381);
}

void RenderArea::setShape(Shape shape)
{
    this->shape = shape;
    update();
}

void RenderArea::setPen(const QPen &pen)
{
    this->pen = pen;
    update();
}

void RenderArea::setBrush(const QBrush &brush)
{
    this->brush = brush;
    update();
}

void RenderArea::setAntialiased(bool antialiased)
{
    this->antialiased = antialiased;
    update();
}

void RenderArea::setTransformed(bool transformed)
{
    this->transformed = transformed;
    update();
}

void RenderArea::paintEvent(QPaintEvent * /* event */)
{
    static const QPoint points[3] = {
             QPoint(10, 80),
             QPoint(20, 10),
             QPoint(90, 70)
         };

    QRect rect(10, 20, 30, 30);


    QPainter painter(this);
    painter.setPen(pen);
    painter.setBrush(brush);

    if (antialiased)
        painter.setRenderHint(QPainter::Antialiasing, true);

    for (int x = 0; x < width(); x += 100) {
        for (int y = 0; y < height(); y += 100) {
            painter.save();
            painter.translate(x, y);
            if (transformed) {
                painter.translate(50, 50);
                painter.rotate(60.0);
                painter.scale(0.6, 0.9);
                painter.translate(-50, -50);
            }

            switch (shape)
            {
            case Square:
                painter.drawRect(rect);
                break;
            case Circle:
                painter.drawEllipse(rect);
                break;
            case Triangle:
                painter.drawPolyline(points,3);
                break;
            }

            painter.restore();
        }
    }

    painter.setRenderHint(QPainter::Antialiasing, false);
    painter.setPen(palette().dark().color());
    painter.setBrush(Qt::NoBrush);



    painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
}
